/*
 * Yabumi, a 2d scrolling shoot-em-up game.
 * Copyright (C) 2003 Gemma Teruaki <terugen@yabumi.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <SDL.h>
#include "../taskmanager.h"
#include "../sprite.h"
#include "../video.h"
#include "../util.h"
#include "../task/hitcircle.h"
#include "../task/player.h"
#include "../task/enemy.h"
#include "../task/musashi.h"
#include "../task/missile.h"
#include "../task/flame.h"
#include "../task/homing.h"
#include "../task/explosion.h"
#include "../task/smoke.h"
#include "../task/homing_tail.h"
#include "../task/item.h"
#include "title.h"
#include "game.h"

int Mapdata[MAP_W][MAP_H];
static enum {__game, __title, __pause} game_flag = __game;

static void Handle_Event();

task Scene_Game(_option *optv)
{
  if (game_flag == __title) {
    game_flag = __game;
    Sprite_Free();
    Hitcircle_Free();
    Player_Free();
    Enemy_Free();
    Musashi_Free();
    Missile_Free();
    Flame_Free();
    Homing_Free();
    Explosion_Free();
    Smoke_Free();
    Homing_Tail_Free();
    Item_Free();
    Task_Free();
    Task_Add(Scene_Title, NULL, NULL, INFINITY_COUNT);
    return;
  };
  if (game_flag == __pause) {
    while (game_flag == __pause) {
      Read_Event();
      if (Key_Register.p == true) {
	Key_Register.p = false;
	game_flag = __game;
      };
      LoopWait();
    };
  };
  int i,j;
  if (optv->count == INFINITY_COUNT) {
    Sprite_Init();
    Hitcircle_Init();
    Player_Init();
    Enemy_Init();
    Musashi_Init();
    Missile_Init();
    Flame_Init();
    Homing_Init();
    Explosion_Init();
    Smoke_Init();
    Homing_Tail_Init();
    Item_Init();
    /* Load MAP data */
    for (i = 0; i < MAP_W; i++) {
      for (j = 0;j < MAP_H; j++) {
	Mapdata[i][j] = 2;
      };
    }; 
    Mapdata[6][6] = 0;
    Mapdata[6][7] = 0;
    Mapdata[6][8] = 0;
    Mapdata[6][9] = 0;
    Mapdata[7][6] = 0;
    Mapdata[7][7] = 1;
    Mapdata[7][8] = 1;
    Mapdata[7][9] = 0;
    Mapdata[8][6] = 0;
    Mapdata[8][7] = 1;
    Mapdata[8][8] = 1;
    Mapdata[8][9] = 0;
    Mapdata[9][6] = 0;
    Mapdata[9][7] = 0;
    Mapdata[9][8] = 0;
    Mapdata[9][9] = 0;
    /* Load MAP data */

    Task_Add(Background_Draw, NULL, NULL, INFINITY_COUNT);
    Task_Add(Player_Add, Player_Create(300, 200, 0, 0, 0), NULL, ONETIME_COUNT);
    Camera.x = 300;
    Camera.y = 200;
    return;
  };
  
  if (optv->count == -60) {
    int z;
    for (z = 0; z < 6; z++) {
      Task_Add(Enemy_Add, Enemy_Create(200, 200, 0, 0, z * 60), NULL, ONETIME_COUNT);
    };
  };
  if (optv->count == -200) {
    Task_Add(Musashi_Add, Musashi_Create(2000, 2000, 0, 0, 0), NULL, ONETIME_COUNT);
  };
  if (optv->count == -1500) {
    Task_Add(Musashi_Add, Musashi_Create(-2000, -2000, 0, 0, 0), NULL, ONETIME_COUNT);
  };
  
  Handle_Event();

  _list_item *list_item = player_list_header.next->next;
  if (list_item == player_list_header.prev) return;
  _Player *player = &list_item->data.Player;

  Camera.x = player->x;
  Camera.y = player->y;
  Video_Sprites_Draw();
}

static void Handle_Event()
{
  _list_item *list_item = player_list_header.next->next;
  if (list_item == player_list_header.prev) return;
  _Player *player = &list_item->data.Player;

  if (Key_Register.space == 1) player->tasks.fire = true;
  if (Key_Register.up == 1) player->tasks.fox1 = true;
  if (Key_Register.down == 1) player->tasks.thrust = true;
  if (Key_Register.left == 1) player->tasks.rotate_left = true;
  if (Key_Register.right == 1) player->tasks.rotate_right = true;
  if (Key_Register.q == true) {
    Key_Register.q = false;
    game_flag = __title;
  };
  if (Key_Register.z == true) {
    Key_Register.z = false;
    Task_Add(Enemy_Add, Enemy_Create(200, 200, 0, 0, 0), NULL, ONETIME_COUNT);
  };
  if (Key_Register.x == true) {
    Key_Register.x = false;
    int z;
    for (z = 0; z < 6; z++) {
      Task_Add(Enemy_Add, Enemy_Create(200, 200, 0, 0, z * 60), NULL, ONETIME_COUNT);
    };
  };
  if (Key_Register.c == true) {
    Key_Register.c = false;
    Task_Add(Player_Add, Player_Create(300, 200, 0, 0, 0), NULL, ONETIME_COUNT);
  };
  if (Key_Register.p == true) {
    Key_Register.p = false;
    game_flag = __pause;
  };
}

task Background_Draw(_option *optv)
{
  int i, j;
  int x, y;
  int k;
  SDL_Surface *image;
  for (i = 0; i < MAP_W; i++) {
    for (j = 0; j < MAP_H; j++) {
      x = i * SPRITE_WH;
      y = j * SPRITE_WH;
      k = Mapdata[i][j];
      image = Sprite_Background[k];
      Sprite_Add(x, y, Z_ORDER_D3, image);
    };
  };
}

